package gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;

import DlgUtils.*;
import TimerLogs.*;
import Images.*;
import Resizers.*;

public class SettingDlg {
	private Display _display;
	private Shell _shell;
	private MemoryImage _image;
	private CanvasDlg _canvas;
	private Label _logLabel;
	private Combo _resizersCombo;
	private final String[] _filterNames =  new String []{"jpg", "gif", "png", "bmp", "All Files (*.*)"};
	private final String[] _filterExtensions = new String [] {"*.jpg;*.jpeg;*.JPG;*.JPEG", "*.gif;*.GIF", "*.png;*.PNG", "*.bmp;*.BMP;*.bitmap;*.BITMAP","*"};
	private final int[] _filterTypes = new int[] {SWT.IMAGE_JPEG, SWT.IMAGE_PNG, SWT.IMAGE_PNG, SWT.IMAGE_BMP, SWT.IMAGE_BMP};
	private Resizer[] _resizerTypes = new Resizer[] {new NNResizer(), new BLResizer(), new SeamLiveResizer(false), new SeamLiveResizer(true), new SeamResizer()};
	private String[] _resizerNames = new String[] {"Nearest Neighbor", "BiLinear Aproximation", "Live horizontal seam carving", "Live vertical seam carving", "The SEAM CRVING!"};
			
	enum Method {NearestNeighbor, BiLinearAproximation, SeamCarving};
	
	public Method CurrentMethod;
	
	public boolean IsDisposed()
	{
		return _shell.isDisposed();
	}
	
	public Label LoggingLabel()
	{
		return _logLabel;
	}
	
	private void SetInCenter()
	{
		Rectangle bounds = _display.getPrimaryMonitor().getBounds();
		_shell.setLocation (bounds.height / 2, bounds.width / 2);
	}
	
	public void Show()
	{
		
			
		_shell.pack();
		_shell.open();
	}
	
	public SettingDlg(Display myDisplay, MemoryImage image, CanvasDlg canvas)
	{
		_image = image;
		_display = myDisplay;
		_shell = new Shell(_display);
		_canvas = canvas;
		
		Init();
		
		for (Resizer r : _resizerTypes)
			r.SetLogger(new LabelLog(_logLabel));
	}
	
	private void Init()
	{
		SetInCenter();
		
		_shell.setText("Computer Graphics Ex1. Image Resize. Haim Bender & Ilia Gorelik");
		_shell.setLayout(new GridLayout(3, false));
		
		//Open Button
		Button button = new Button(_shell, SWT.None);
		button.setText("Open");
		button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		button.addMouseListener(
				new MouseListener()
				{
					public void mouseDoubleClick(MouseEvent e){}
					public void mouseDown(MouseEvent e){}
					public void mouseUp(MouseEvent e)
					{
						FileDialog dialog = new FileDialog (_shell, SWT.OPEN);
						dialog.setFilterNames (_filterNames);
						dialog.setFilterExtensions (_filterExtensions);
						dialog.setFileName ("");
						String fileName = dialog.open();
						if (fileName != null)
						{
							if (!_canvas.Opened())
								_canvas.Show();
							
							_image.Open(fileName);
							_canvas.Resize(_canvas.OriginalSize());
						}
					}
				});
		
		//Resize Button
		button = new Button(_shell, SWT.None);
		button.setText("Resize Now");
		button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2));
		button.addMouseListener(
				new MouseListener()
				{
					public void mouseDoubleClick(MouseEvent e){}
					public void mouseDown(MouseEvent e){}
					public void mouseUp(MouseEvent e)
					{
						if (!_canvas.Opened())
							return;
						
						Rectangle bounds = _canvas.GetBounds();
						(new GetSizeDlg(_display, new Point(bounds.width, bounds.height), _canvas.OriginalSize(), 
								new GetSizeDlg.Runer()
								{
									public void OnClick(Point newSize)
									{
										_canvas.Resize(newSize);
									}
								}
						)).Show();
					}
				});
		
		//Method compsit.
		Composite composit = new Composite(_shell, SWT.NONE);
		composit.setLayout(new GridLayout(3, false));
		
			//Label which says "Method:"
			Label label = new Label(composit, SWT.None);
			label.setText("Method:");
			label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true));
			
			_resizersCombo = new Combo(composit, SWT.None);
			
			{
				int resizerIndex = 0;
				for (String resizerName : _resizerNames)
					_resizersCombo.add(resizerName, resizerIndex++);
			}
				
			_resizersCombo.select(0);
			_resizersCombo.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true));
			_resizersCombo.addSelectionListener(
					new SelectionListener()
					{
						public void widgetSelected(SelectionEvent e)
						{
							MessageBox message = new MessageBox(_shell, SWT.YES | SWT.NO);
							message.setMessage("Do you want to resize the current image(YES) or reload the original?(NO)");
							if (message.open() == SWT.YES)
							{
								Rectangle bounds = _canvas.GetBounds();
								_image.ResizeOriginal(_canvas.CanResize(new Point(bounds.width, bounds.height)));
							}
							
							Point screenSize = Functions.GetScreenSize(_display);
							_resizerTypes[_resizersCombo.getSelectionIndex()]
							             .Init(_image.GetOriginalImage(), new Point(Math.min(800, screenSize.x), Math.min(800, screenSize.y)));
						
							_image.setResizer(_resizerTypes[_resizersCombo.getSelectionIndex()]);
							
							if (_resizersCombo.getSelectionIndex() == 4)
							{
								Rectangle bounds = _canvas.GetBounds();
								(new GetSizeDlg(_display, new Point(bounds.width, bounds.height), _canvas.OriginalSize(),
										new GetSizeDlg.Runer()
										{
											public void OnClick(Point newSize)
											{
												_canvas.Resize(newSize);
												_image.setResizer(new BLResizer());
												_resizersCombo.select(1);
											}
										})).Show();
							}
							else
								_canvas.Resize(_image.OriginalSize());
						}
						
						public void widgetDefaultSelected(SelectionEvent e){}
					}
			);
		
		//Save Button
		button = new Button(_shell, SWT.None);
		button.setText("Save");
		button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		button.addMouseListener(
				new MouseListener()
				{
					public void mouseDoubleClick(MouseEvent e){}
					public void mouseDown(MouseEvent e){}
					public void mouseUp(MouseEvent e)
					{
						if (!_canvas.Opened())
							return;
						
						FileDialog dialog = new FileDialog (_shell, SWT.SAVE);
						dialog.setFilterNames (_filterNames);
						dialog.setFilterExtensions (_filterExtensions); 
						dialog.setFileName ("");
						String fileName = dialog.open();
						if (fileName != null)
						{
							Rectangle bounds = _canvas.GetBounds();
							_image.Save(fileName, _filterTypes[dialog.getFilterIndex()]
							                                   , new Point(bounds.width, bounds.height));
						}
					}
				});

		
		//Live Resize Composit
		composit = new Composite(_shell, SWT.None);
		composit.setLayout(new GridLayout(3, false));
		/*
			//Resize CheckBox
			button = new Button(composit, SWT.CHECK);
			button.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true));
			
			//Resize Message
			label = new Label(composit, SWT.None);
			label.setText("Resize with window");
			label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true));
		*/
		
		_logLabel = new Label(_shell, SWT.BORDER);
		_logLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 0));
	}
}
